package com.gongpingjialoan.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.data.CategoryData;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.utility.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/5/26.
 */
public class SubScribeAdapter extends BaseAdapter {


    private Context mContext;

    private CategoryData mCategoryData;

    private LayoutInflater mLayoutInflater;

    List<JSONObject> list;

    //用途    (多选)
    private String[] show_intent = {"微型车", "小型车", "中级车", "中高级车", "SUV", "跑车", "豪华车", "商务车"};
    private String[] need_intent = {"mini_car", "small_car,compact_car", "middle_car", "middle_super_car", "suv", "super_car", "luxury_car", "mpv"};


    public SubScribeAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mCategoryData = GPJApplication.getInstance().getCategoryData();
    }

    public void setData(List<JSONObject> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {

        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public JSONObject getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_subscribe, null);
            holder = new ViewHolder();
            holder.priceT = (TextView) convertView.findViewById(R.id.price);
            holder.tuisongT = (TextView) convertView.findViewById(R.id.tuisong);
            holder.otherT = (TextView) convertView.findViewById(R.id.other);
            holder.use_typeT = (TextView) convertView.findViewById(R.id.use_type);
            holder.car_typeT = (TextView) convertView.findViewById(R.id.car_type);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }

        JSONObject jo
                = getItem(position);
        try {
            String price = jo.getString("price_l") + "-" + jo.getString("price_h") + "万";
            holder.priceT.setText(Utils.changeColor(mContext, "预        算: " + price, 12, 12 + price.length(), R.color.text_grey_dark));
        } catch (JSONException e) {
            e.printStackTrace();
        }


        try {
            JSONArray vehicle_modelJsa = jo.getJSONArray("vehicle_model");
            String cartype = "";
            if (vehicle_modelJsa != null) {
                for (int i = 0; i < vehicle_modelJsa.length(); i++) {
                    if (TextUtils.isEmpty(cartype)) {

                        cartype = cartype + getCarty(vehicle_modelJsa.get(i).toString());
                    } else {
                        if(!cartype.contains(getCarty(vehicle_modelJsa.get(i).toString()))){
                            cartype = cartype + "," + getCarty(vehicle_modelJsa.get(i).toString());
                        }



                    }
                }
            }
            holder.car_typeT.setText(Utils.changeColor(mContext, "意向车型: " + cartype, 6, 6 + cartype.length(), R.color.text_grey_dark));
        } catch (JSONException e) {
            e.printStackTrace();
        }


        try {
            JSONArray usesJsa = jo.getJSONArray("uses");
            String uses = "";
            if (usesJsa != null) {
                for (int i = 0; i < usesJsa.length(); i++) {
                    if (TextUtils.isEmpty(uses)) {
                        uses = uses + usesJsa.get(i);
                    } else {
                        uses = uses + "," + usesJsa.get(i);
                    }
                }
            }
            holder.use_typeT.setText(Utils.changeColor(mContext, "购车用途: " + uses, 6, 6 + uses.length(), R.color.text_grey_dark));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            JSONArray otherJsa = jo.getJSONArray("other_recommend");
            String other = "";
            if (otherJsa != null) {
                for (int i = 0; i < otherJsa.length(); i++) {
                    if (TextUtils.isEmpty(other)) {
                        other = other + otherJsa.get(i);
                    } else {
                        other = other + "," + otherJsa.get(i);
                    }
                }
            }
            holder.otherT.setText(Utils.changeColor(mContext, "其他需求: " + other, 6, 6 + other.length(), R.color.text_grey_dark));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            String push_setting = jo.getString("push_setting");
            String phsh_des = "";
            if (push_setting.equals("im")) {
                phsh_des = "发现立即推送";
            } else {
                phsh_des = jo.getString("start_time")+"-"+jo.getString("end_time");
            }
            holder.tuisongT.setText(Utils.changeColor(mContext, "推送类型: " + phsh_des, 6, 6 + phsh_des.length(), R.color.text_red));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return convertView;
    }

    public static class ViewHolder {
        public TextView priceT;
        public TextView tuisongT;
        public TextView car_typeT;
        public TextView use_typeT;
        public TextView otherT;
    }

    private  String  getCarty(String  type) {

        String cartype="";
        for(int i=0;i<need_intent.length;i++) {
            if(need_intent[i].contains(type)){

                cartype =  show_intent[i];
                break;
            }
        }
        return cartype;

    }

}

